﻿/*
* @Author： whg
* @Date : 2021-1-19
* @Desc : 
*/
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using XNode;
public class CameraNode : BaseNode
{
    //public override void Trigger()
    //{
    //    (graph as StoryGraph).current = this;
    //}
    [Input(backingValue = ShowBackingValue.Always)]
    public BaseNode upLink;
    [Output(backingValue = ShowBackingValue.Always)]
    public BaseNode downLink;
    public override StringBuilder ToTable()
    {
        base.ToTable();
        StringBuilder result = new StringBuilder(1024);
        string space = "				";
        result.Append(space+"{\n					type = Config.StoryConst.CameraAnimation,\n");
        if(wait)
        result.Append(space+"	wait = "+wait.ToString().ToLower()+",\n");
        if(isMove)
        {
            result.Append(space + "	Move = \n" + space + "  	{\n");
            result.Append(space + "   		mode = " + (int)CameraMove.motionType + ",\n");
            if(CameraMove.motionType != MotionType.直接切换)
            result.Append(space + "   		time = " + CameraMove.time + ",\n");
            if(CameraMove.self)
                result.Append(space + "   		self = " + CameraMove.self.ToString().ToLower() + ",\n");
            else
                result.Append(space + "   		target = {x=" + CameraMove.target.x + ",y=" + CameraMove.target.y + ",z=" + CameraMove.target.z + "},\n");
            result.Append(space + "  	},\n");
        }
        if(isRotate)
        {
            result.Append(space + "	Rotate = \n" + space + "  	{\n");
            result.Append(space + "   		mode = " + (int)CameraRotate.motionType + ",\n");
            if (CameraRotate.motionType != MotionType.直接切换)
            result.Append(space + "   		time = " + CameraRotate.time + ",\n");
            if(CameraRotate.restore)
                result.Append(space + "   		restore = " + CameraRotate.restore.ToString().ToLower() + ",\n");
            else
                result.Append(space + "   		target = {x=" + CameraRotate.target.x + ",y=" + CameraRotate.target.y + "},\n");
            result.Append(space + "  	},\n");
        }
        if(isFollow)
        {
            result.Append(space + "	Follow = \n" + space + "  	{\n");
            if(!follow_init)
            result.Append(space + "   		time = " + follow_time + ",\n");
            result.Append(space + "   		init = " + follow_init.ToString().ToLower() + ",\n");
            if (follow_target != "")
            result.Append(space + "   		target = \"" + follow_target + "\",\n");
            if (follow_ins_id != "")
            result.Append(space + "   		ins_id = [[" + follow_ins_id + "]],\n");
            if (RootOffset.ToString(space) != "")
            result.Append(space + "   		RootOffset = \n" + space + "   		{\n" + RootOffset.ToString(space + "   			") + "\n" + space + "   		},\n");
            if (RootRotate.ToString(space) != "")
            result.Append(space + "   		RootRotate = \n" + space + "   		{\n" + RootRotate.ToString(space + "   			") + "\n" + space + "   		},\n");
            if (ChildOffset.ToString(space) != "")
            result.Append(space + "   		ChildOffset = \n" + space + "   		{\n" + ChildOffset.ToString(space + "   			") + "\n" + space + "   		},\n");
            if (ChildRotate.ToString(space) != "")
            result.Append(space + "   		ChildRotate = \n" + space + "   		{\n" + ChildRotate.ToString(space + "   			") + "\n" + space + "   		},\n");
            if (Lookat.ToString(space) != "")
            result.Append(space + "   		Lookat = \n" + space + "   		{\n" + Lookat.ToString(space + "   			") + "\n" + space + "   		},\n");
            result.Append(space + "  	},\n");
        }
        if(isZoom)
        {
            result.Append(space + "	Zoom = \n" + space + "  	{\n");
            result.Append(space + "   		mode = " + (int)Zoom_MotionType + ",\n");
            if (Zoom_MotionType != MotionType.直接切换)
            result.Append(space + "   		time = " + Zoom_time + ",\n");
            result.Append(space + "   		target = " + Zoom_target + "\n");
            result.Append(space + "  	},\n");
        }
        if(isShake)
        {
            result.Append(space + "	Shake = \n" + space + "  	{\n");
            result.Append(CameraShake.ToString(space + "   		"));
            result.Append(space + "  	},\n");
        }
        result.Append(space+"},");
        return result;
    }
    //public enum AnimatType { Move, Rotate, Follow, Zoom, Shake }
    public enum MotionType { 直接切换 = 0, 匀速行进, 缓速行进, 加速行进 }
    public enum ShakeType { x方向, y方向, z方向, x旋转, y旋转, z旋转, 拉伸 }
    [Tooltip(" 等待当前项执行完才进行下一项: wait = true\n当为false时，将会在一帧中继续执行下一片段")]
    public bool wait = false;
    #region Move
    public bool isMove = false;
    public CameraAnimatMove CameraMove;
    #endregion

    #region Rotate
    public bool isRotate = false;
    public CameraAnimatRotate CameraRotate;
    #endregion

    #region Follow
    public bool isFollow = false;
    //public CameraAnimatFollow CameraFollow;
    [Tooltip("跟随时间（-1为一直跟随），直接切换不用填")]
    public float follow_time;
    [Tooltip("选填。需要直接设置跟随位置时")]
    public bool follow_init = true;
    [Tooltip("跟随类型，Actor, MainRole")]
    public string follow_target;
    [Tooltip("类型为Actor必填")]
    public string follow_ins_id;
    [Tooltip("相机相对跟随对象的位置")]
    public CameraFollowSet RootOffset;
    [Tooltip("相机相对跟随对象的位置")]
    public CameraFollowSet RootRotate;
    [Tooltip("相机相对跟随对象的位置")]
    public CameraFollowSet ChildOffset;
    [Tooltip("相机相对跟随对象的位置")]
    public CameraFollowSet ChildRotate;
    public CameraFollowSet Lookat;
    [System.Serializable]
    public class CameraFollowSet
    {
        public float speed;
        public Vector3 location;
        public string ToString(string str)
        {
            string result = "";
            if (location != Vector3.zero)
            {
                result = str + "x=" + location.x + ",y=" + location.y + ",z=" + location.z;
                if (speed != 0)
                    result += ",\n" + str + "speed = " + speed + ",";
            }
            return result;
        }
    }
    #endregion

    #region Zoom
    public bool isZoom = false;
    public MotionType Zoom_MotionType = MotionType.直接切换;
    [Tooltip("缩放时间，直接切换不用填")]
    public float Zoom_time = 1;
    [Tooltip("缩放目标值")]
    public float Zoom_target = 35;
    #endregion

    #region Shake
    public bool isShake = false;
    public CameraAnimatShake CameraShake;
    #endregion
    
    [System.Serializable]
    public class CameraAnimatMove 
    {
        public MotionType motionType = MotionType.直接切换;
        [Tooltip("选填,是否移到动主角位置,true,则移动到主角的位置,target参数无效")]
        public bool self = false;
        [Tooltip("移动时间，直接切换不用填")]
        public float time = 1;
        [Tooltip("移动到的目标位置,实际场景中的位置")]
        public Vector3 target;
    }
    [System.Serializable]
    public class CameraAnimatRotate 
    {
        public MotionType motionType = MotionType.直接切换;
         [Tooltip("旋转时间，直接切换不用填")]
        public float time = 1;
         [Tooltip("选填。旋转至注视主角。true,则target参数无效")]
        public bool lookat = false;
         [Tooltip("回复锁定视角旋转角度")]
        public bool restore = false;
        [Tooltip("进行相机根节点旋转")]
        public bool root = false;
         [Tooltip("目标角度,只支持x，y轴旋转")]
        public Vector2 target;
    }
    [System.Serializable]
    public class CameraAnimatFollow
    {
        [Tooltip("跟随时间（-1为一直跟随），直接切换不用填")]
        public float time;
         [Tooltip("选填。需要直接设置跟随位置时")]
        public bool init = true;
        [Tooltip("跟随类型，Actor, MainRole")]
        public string target = "Actor";
        [Tooltip("类型为Actor必填")]
        public string ins_id = "";
        [Tooltip("相机相对跟随对象的位置")]
        public CameraFollowSet RootOffset;
        [Tooltip("相机相对跟随对象的位置")]
        public CameraFollowSet RootRotate;
        [Tooltip("相机相对跟随对象的位置")]
        public CameraFollowSet ChildOffset;
        [Tooltip("相机相对跟随对象的位置")]
        public CameraFollowSet ChildRotate;
        public CameraFollowSet Lookat;
        [System.Serializable]
        public class CameraFollowSet
        {
            public float speed;
            public Vector3 location;
            public string ToString(string str)
            {
                string result = "";
                if(location != Vector3.zero)
                {
                     result = str+"x="+location.x+",y="+location.y+",z="+location.z;
                    if(speed!=0)
                        result +=",\n"+ str+"speed = "+speed+",";
                }
                return result;
            }
        }
    }
    [System.Serializable]
    public class CameraAnimatZoom
    {
        public MotionType motionType = MotionType.直接切换;
        [Tooltip("缩放时间，直接切换不用填")]
        public float time = 1;
        [Tooltip("缩放目标值")]
        public float target;
    }
    [System.Serializable]
    public class CameraAnimatShake
    {
        public string ToString(string str)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < shakeList.Count; i++)
            {
                CameraShakeSet shake = shakeList[i];
                sb.Append(str + "{shake_start_time = " + shake.shake_start_time +",");
                sb.Append("time = " + shake.time + ",");
                sb.Append("mode = " + shake.mode + ",");
                sb.Append("extent = " + shake.extent + ",");
                sb.Append("shake_angle = " + shake.shake_angle + ",");
                sb.Append("start_angle = " + shake.start_angle + "},\n");
            }
            return sb.ToString();
        }
        public List<CameraShakeSet> shakeList = new List<CameraShakeSet>();
        [System.Serializable]
        public class CameraShakeSet
        {
            public ShakeType shakeType = ShakeType.x方向;
             [Tooltip("开始时间")]
            public float shake_start_time;
             [Tooltip("持续时间")]
            public float time;
             [Tooltip("模式")]
            public float mode;
             [Tooltip("震动幅度")]
            public float extent;
             [Tooltip("震动周期总共角度")]
            public float shake_angle;
             [Tooltip(" 震动周期开始角度")]
            public float start_angle;
        }
    }
}

